<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建资源流上下文</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-context-create" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_context_create</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">stream_context_create</span> &mdash; <span class="dc-title">创建资源流上下文</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-context-create-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_context_create</strong></span>
    ([ <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$params</code></span>
  ]] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   创建并返回一个资源流上下文，该资源流中包含了 <code class="parameter">options</code> 中提前设定的所有参数的值。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-context-create-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">options</code></dt>

     <dd>

      <p class="para">
       必须是一个二维关联数组，格式如下：<em>$arr[&#039;wrapper&#039;][&#039;option&#039;] = $value</em> 。
      </p>
      <p class="para">
       默认是一个空数组。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">params</code></dt>

     <dd>

      <p class="para">
       必须是 <em>$arr[&#039;parameter&#039;] = $value</em> 格式的关联数组。
       请参考 <a href="context.html" class="link">context parameters</a> 里的标准资源流参数列表。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-context-create-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   上下文资源流，类型为 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> 。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-context-create-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        增加了可选参数 <code class="parameter">params</code> 。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-context-create-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4813">
   <p><strong>Example #1 使用 <span class="function"><strong>stream_context_create()</strong></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'http'</span><span style="color: #007700">=&gt;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'header'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"Accept-language:&nbsp;en\r\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Cookie:&nbsp;foo=bar\r\n"<br />&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Sends&nbsp;an&nbsp;http&nbsp;request&nbsp;to&nbsp;www.example.com<br />&nbsp;&nbsp;&nbsp;with&nbsp;additional&nbsp;headers&nbsp;shown&nbsp;above&nbsp;*/<br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'r'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fpassthru</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.stream-context-create-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="stream_context_set_option.html" class="function" rel="rdfs-seeAlso">stream_context_set_option()</a> - 对资源流、数据包或者上下文设置参数</span></li>
   <li class="member">Listing of supported wrappers (<a href="wrappers.html" class="xref">支持的协议和封装协议</a>)</li>
   <li class="member">Context options (<a href="context.html" class="xref">上下文（Context）选项和参数</a>)</li>
  </ul>
 </div>

</div></div></div></body></html>